package optiondialoges;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.Serializable;

import javax.swing.JComboBox;

import panel.GlyphPanel;


public class TranslateOptionPanel extends JDialog implements ActionListener,
        WindowListener, Serializable {

    private static final long serialVersionUID = 1L;

    private JPanel jContentPane = null;

    private JPanel buttonPanel = null;

    private JPanel optionPanel = null;

    private JButton okButton = null;

    private JButton cancelButton = null;

    private JLabel xOffsetLabel = null;

    private JTextField xOffsetTextField = null;

    private JLabel yOffsetLabel = null;

    private JTextField yOffsetTextField = null;

    private JLabel interploationLabel = null;

    private JComboBox interpolationComboBox = null;

    private GlyphPanel panel;

    private String xOffset;

    private String yOffset;

    private String interpolation;

    /**
     * @param owner
     */
    public TranslateOptionPanel(Frame owner, GlyphPanel panel) {
        super(owner);
        this.panel = panel;
        this.addWindowListener(this);
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setSize(300, 200);
        this.setContentPane(getJContentPane());
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane() {
        if (jContentPane == null) {
            jContentPane = new JPanel();
            jContentPane.setLayout(new BorderLayout());
            jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
            jContentPane.add(getOptionPanel(), BorderLayout.CENTER);
        }
        return jContentPane;
    }

    /**
     * This method initializes buttonPanel
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getButtonPanel() {
        if (buttonPanel == null) {
            GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
            gridBagConstraints1.gridx = 1;
            gridBagConstraints1.insets = new Insets(0, 10, 10, 10);
            gridBagConstraints1.gridy = 0;
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.insets = new Insets(0, 10, 10, 10);
            buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridBagLayout());
            buttonPanel.add(getOkButton(), gridBagConstraints);
            buttonPanel.add(getCancelButton(), gridBagConstraints1);
        }
        return buttonPanel;
    }

    /**
     * This method initializes optionPanel
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getOptionPanel() {
        if (optionPanel == null) {
            GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
            gridBagConstraints7.fill = GridBagConstraints.NONE;
            gridBagConstraints7.gridy = 2;
            gridBagConstraints7.weightx = 1.0;
            gridBagConstraints7.insets = new Insets(10, 0, 0, 0);
            gridBagConstraints7.anchor = GridBagConstraints.WEST;
            gridBagConstraints7.gridx = 1;
            GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
            gridBagConstraints6.gridx = 0;
            gridBagConstraints6.anchor = GridBagConstraints.EAST;
            gridBagConstraints6.insets = new Insets(10, 10, 0, 10);
            gridBagConstraints6.gridy = 2;
            interploationLabel = new JLabel();
            interploationLabel.setText("Interpolation");
            GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
            gridBagConstraints5.fill = GridBagConstraints.VERTICAL;
            gridBagConstraints5.gridy = 1;
            gridBagConstraints5.weightx = 1.0;
            gridBagConstraints5.anchor = GridBagConstraints.WEST;
            gridBagConstraints5.insets = new Insets(10, 0, 0, 0);
            gridBagConstraints5.gridx = 1;
            GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
            gridBagConstraints4.gridx = 0;
            gridBagConstraints4.insets = new Insets(10, 10, 0, 10);
            gridBagConstraints4.anchor = GridBagConstraints.EAST;
            gridBagConstraints4.gridy = 1;
            yOffsetLabel = new JLabel();
            yOffsetLabel.setText("Y Offset");
            GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
            gridBagConstraints3.fill = GridBagConstraints.VERTICAL;
            gridBagConstraints3.gridy = 0;
            gridBagConstraints3.weightx = 1.0;
            gridBagConstraints3.anchor = GridBagConstraints.WEST;
            gridBagConstraints3.insets = new Insets(10, 0, 0, 0);
            gridBagConstraints3.gridx = 1;
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
            gridBagConstraints2.gridx = 0;
            gridBagConstraints2.anchor = GridBagConstraints.EAST;
            gridBagConstraints2.insets = new Insets(10, 10, 0, 10);
            gridBagConstraints2.gridy = 0;
            xOffsetLabel = new JLabel();
            xOffsetLabel.setText("X Offset");
            optionPanel = new JPanel();
            optionPanel.setLayout(new GridBagLayout());
            optionPanel.add(xOffsetLabel, gridBagConstraints2);
            optionPanel.add(getXOffsetTextField(), gridBagConstraints3);
            optionPanel.add(yOffsetLabel, gridBagConstraints4);
            optionPanel.add(getYOffsetTextField(), gridBagConstraints5);
            optionPanel.add(interploationLabel, gridBagConstraints6);
            optionPanel.add(getInterpolationComboBox(), gridBagConstraints7);
        }
        return optionPanel;
    }

    /**
     * This method initializes okButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getOkButton() {
        if (okButton == null) {
            okButton = new JButton();
            okButton.setText("Ok");
            okButton.addActionListener(this);
        }
        return okButton;
    }

    /**
     * This method initializes cancelButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getCancelButton() {
        if (cancelButton == null) {
            cancelButton = new JButton();
            cancelButton.setText("Cancel");
            cancelButton.addActionListener(this);
        }
        return cancelButton;
    }

    /**
     * This method initializes xOffsetTextField
     * 
     * @return javax.swing.JTextField
     */
    private JTextField getXOffsetTextField() {
        if (xOffsetTextField == null) {
            xOffsetTextField = new JTextField();
            xOffsetTextField.setPreferredSize(new Dimension(120, 21));
        }
        return xOffsetTextField;
    }

    /**
     * This method initializes yOffsetTextField
     * 
     * @return javax.swing.JTextField
     */
    private JTextField getYOffsetTextField() {
        if (yOffsetTextField == null) {
            yOffsetTextField = new JTextField();
            yOffsetTextField.setPreferredSize(new Dimension(120, 21));
        }
        return yOffsetTextField;
    }

    /**
     * This method initializes interpolationComboBox
     * 
     * @return javax.swing.JComboBox
     */
    private JComboBox getInterpolationComboBox() {
        if (interpolationComboBox == null) {
            String[] labels = { "None", "Bilinear", "Bicubic" };
            interpolationComboBox = new JComboBox(labels);
            interpolationComboBox.setPreferredSize(new Dimension(120, 26));
        }
        return interpolationComboBox;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();
        if (button.getText().contentEquals("Ok")) {
            xOffset = xOffsetTextField.getText();
            yOffset = yOffsetTextField.getText();
            interpolation = (String) interpolationComboBox.getSelectedItem();
            String command = String.format(
                    "run(\"Translate...\", \"x=%s y=%s interpolation=%s\");",
                    xOffset, yOffset, interpolation);
            panel.setCommand(command);
            panel.setDialog(this);
            panel.setOptionsSet(true);
        } else if (button.getText().contentEquals("Cancel")) {
            xOffsetTextField.setText(xOffset);
            yOffsetTextField.setText(yOffset);
            interpolationComboBox.setSelectedItem(interpolation);
        }
        this.setVisible(false);

    }

    @Override
    public void windowActivated(WindowEvent e) {
        // ignore

    }

    @Override
    public void windowClosed(WindowEvent e) {
        // ignore

    }

    @Override
    public void windowClosing(WindowEvent e) {
        // ignore

    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        xOffsetTextField.setText(xOffset);
        yOffsetTextField.setText(yOffset);
        interpolationComboBox.setSelectedItem(interpolation);
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        // ignore

    }

    @Override
    public void windowIconified(WindowEvent e) {
        // ignore

    }

    @Override
    public void windowOpened(WindowEvent e) {
        xOffset = xOffsetTextField.getText();
        yOffset = yOffsetTextField.getText();
        interpolation = (String) interpolationComboBox.getSelectedItem();

    }

}
